import { ComponentLoaderFactory } from './ComponentLoaderFactory';
import { MessageComponent } from './message.component';
import { ComponentLoader } from './ComponentLoader';
import { Injectable, Inject, Injector } from '@angular/core';

@Injectable()
export class MessageService {
    private loader: ComponentLoader<MessageComponent>

    constructor(private _clf: ComponentLoaderFactory, private _injector: Injector) {
        this.loader = this._clf.create<MessageComponent>();
    }

    private createMessage(t, c, duration = 2000) {
        this.loader.attch(MessageComponent).to('body');
        const opts = {
            msgType: t,
            payload: c,

        };
        const ref = this.loader.create(opts);
        ref.changeDetectorRef.markForCheck();
        ref.changeDetectorRef.detectChanges();
        let self = this;
        let st = setTimeout(() => {
            self.loader.remove(ref);
        }, duration);
    }

    public info(payload, duration?) {
        this.createMessage('info', payload, duration);
    }

    public success(payload, duration?) {
        this.createMessage('success', payload, duration);
    }

    public error(payload, duration?) {
        this.createMessage('error', payload, duration);
    }

    public warning(payload, duration?) {
        this.createMessage('warning', payload, duration);
    }
}